package cn.whdream.juc;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * 
 * @author admin lock显示锁
 */
public class TestLock {
	public static void main(String[] args) {
		CountDownLatch cuCountDownLatch = new CountDownLatch(5);
		Ticket c = new Ticket(cuCountDownLatch);
	
			new Thread(c, "售票窗口1").start();
			new Thread(c, "售票窗口2").start();
			new Thread(c, "售票窗口3").start();
		}
	

	private static class Ticket implements Runnable {
		private int tick = 100;
		private Lock lock = new ReentrantLock();

		public Ticket(CountDownLatch cu) {
			super();

		}

		@Override
		public void run() {
			for (int i = 0;;) {
				lock.lock();
				try {

					if (tick > 0) {
						Thread.sleep(200);
						System.out.println(Thread.currentThread().getName() + "result:" + (--tick));
					} else {
						break;
					}

				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} finally {
					lock.unlock();
				}
			}
		}

	}

}
